﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.BusinessObject;
using pwblog.Entities;

namespace pwblog.Web
{
    public struct MenuStruct
    {
        private int? parentID;
        private List<pwblog.Entities.Menu> childrenMenu;

        public int? ParentID
        {
            get { return parentID; }
            set { parentID = value; }
        }
        public List<pwblog.Entities.Menu> ChildrenMenu
        {
            get { return childrenMenu; }
            set { childrenMenu = value; }
        }
    }
    public partial class bloglayout : System.Web.UI.MasterPage
    {
        protected List<pwblog.Entities.Menu> menus;
        protected List<MenuStruct> subMenus;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadMenus();
            SortMenus();
        }

        private void LoadMenus()
        {
            MenuBo menubo = new MenuBo();
            pwblog.Entities.Menu[] tmpMenus = menubo.GetMenus();
            if (tmpMenus.Length > 0)
            {
                menus = new List<Entities.Menu>();
                subMenus = new List<MenuStruct>();
                for (int i = 0; i < tmpMenus.Length; i++)
                {
                    if (tmpMenus[i].Parent == null)
                    {
                        menus.Add(tmpMenus[i]);
                    }
                    else
                    {
                        foreach (MenuStruct ms in subMenus)
                        {
                            if (tmpMenus[i].Parent == ms.ParentID)
                            {
                                ms.ChildrenMenu.Add(tmpMenus[i]);
                            }
                            else
                            {
                                MenuStruct newMs = new MenuStruct();
                                newMs.ParentID = tmpMenus[i].Parent;
                                newMs.ChildrenMenu.Add(tmpMenus[i]);
                                subMenus.Add(newMs);
                            }
                        }
                    }
                }
            }
        } /* End LoadMenus */

        private void SortMenus() 
        {
            SortMenu(menus);
            foreach (MenuStruct ms in subMenus)
            {
                SortMenu(ms.ChildrenMenu);
            }
        } /* End SortMenus */

        private void SortMenu(List<pwblog.Entities.Menu> menu)
        {
            if (menu.Count > 0)
            {
                List<pwblog.Entities.Menu> sortedMenu = new List<Entities.Menu>();
                while (menu.Count > 0)
                {
                    int minOrder = menu[0].SortOrder;
                    pwblog.Entities.Menu tmpMenu = menu[0];
                    foreach (pwblog.Entities.Menu m in menu)
                    {
                        if (m.SortOrder < minOrder)
                        {
                            minOrder = m.SortOrder;
                            tmpMenu = m;
                        }
                    }
                    sortedMenu.Add(tmpMenu);
                    menu.Remove(tmpMenu);
                }

                foreach (pwblog.Entities.Menu m in sortedMenu)
                {
                    menu.Add(m);
                }
            }
        } /* End SortMenu */



        public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl)
        {

            if (Request.IsSecureConnection)

                return string.Format("https://{0}:{1}{2}", Request.Url.Host, Request.Url.Port, Page.ResolveUrl(relativeUrl));

            else

                return string.Format("http://{0}:{1}{2}", Request.Url.Host, Request.Url.Port, Page.ResolveUrl(relativeUrl));

        }
    }
}